package com.hrt.web.orderInfor.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.sql.Timestamp;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.GiftDetail;
import com.hrt.bean.HRTMember;
import com.hrt.bean.OrderDetail;
import com.hrt.bean.OrderInfor;
import com.hrt.vo.OrderDetailVO;
import com.hrt.vo.OrderInforVO;
import com.hrt.web.member.service.HRTMemberService;
import com.hrt.web.orderInfor.dao.WebOrderDao;
import com.hrt.web.orderInfor.service.WebOrderDetailService;
import com.hrt.web.orderInfor.service.WebOrderService;
import com.hrt.web.product.service.GiftDetailService;
@Transactional
@Service
public class WebOrderServiceImpl implements WebOrderService{
	@Autowired 
	private WebOrderDao webOrderDao;
	@Autowired 
	private HRTMemberService hrtMemberService;
	@Autowired 
	private GiftDetailService giftDetailService;
	@Autowired 
	private WebOrderDetailService webOrderDetailService;
	@Override
	public Long totalCount() {
		return webOrderDao.totalCount();
	}

	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<OrderInfor> delResult = webOrderDao.getByIds(ids);
		for (OrderInfor  gi: delResult) {
			gi.setIsdelete(new Integer(1));
			webOrderDao.updateEntity(gi);
		}
		
	}

	@Override
	public void saveOrder(OrderInfor orderInfor) throws Exception {
		webOrderDao.saveEntity(orderInfor);
		
	}

	@Override
	public List queryByPage(int pageIndex, int pageSize) {
		
		return webOrderDao.queryByPage(pageIndex, pageSize);
	}

	@Override
	public void updateOrder(OrderInfor orderInfor) {
		webOrderDao.updateEntity(orderInfor);
		
	}

	@Override
	public OrderInfor getOrderByid(Long orderId) {
		return webOrderDao.getById(orderId);
	}

	@Override
	public List queryByPage(String hql, int pageIndex, int pageSize) {
		return webOrderDao.queryByPage(hql, pageIndex, pageSize);
	}
	/**
	 * 根据memberId获取订单
	 * @return
	 */
	@Override
	public Long getOrderInforByMemberId(Long memberId,Integer status) {
		return webOrderDao.getOrderInforByMemberId(memberId,status);
	}

	@Override
	public Timestamp getDate() {
		return webOrderDao.getDate();
	}
	
	/**
	 *将OrderInforVO转换为OrderInfor
	 *实现添加功能
	 * @param vo
	 * @return
	 */
	@Override
	public OrderInfor saveOrderInfor(OrderInforVO vo,List<OrderDetailVO> ods) {
		OrderInfor orderInfor = new OrderInfor();
		try {
			BeanUtils.copyProperties(orderInfor, vo);
			HRTMember member = hrtMemberService.getMemberByName(orderInfor.getMemberName());
			orderInfor.setHrtMember(member);
			for(OrderDetailVO od:ods){
				OrderDetail newDetail = new OrderDetail();
				BeanUtils.copyProperties(newDetail, od);
				
				// 根据礼品规格id获取礼品规格实体
				GiftDetail giftDetail = giftDetailService.getGiftDetailById(od.getGiftDetailId());
				this.saveOrder(orderInfor);
				newDetail.setOrderInfor(orderInfor);
				newDetail.setGiftDetail(giftDetail);
				webOrderDetailService.saveOrderDetail(newDetail);
			};
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return orderInfor;
	}

}
